﻿using System;
using Collaboris.SharePoint.Moss;
using Collaboris.SharePoint.Wss;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace Collaboris.Saf.Actions.Moss.Tests
{
    using SharePoint.Moss.Publishing;
    using SharePoint.Wss.Lists;
    using SharePoint.Wss.Webs;

    public static class TestHelper
    {
        /// <summary>
        /// creates a publishing site
        /// </summary>
        /// <param name="parentWebUrl">
        /// The site collection url.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <param name="webUrl">
        /// The web Url.
        /// </param>
        public static void CreatePublishingWeb(string parentWebUrl, string title, string webUrl)
        {
            using (SPSite site = new SPSite(parentWebUrl))
            {
                using (SPWeb parentWeb = site.OpenWeb())
                {
                    // Create a SOURCE test web
                    SPWebHelper.CreateWeb(parentWeb, true, title, "Unit Test Web", "BLANKINTERNET#0", webUrl, 1033, false, false);
                }
            }
        }

        /// <summary>
        /// Deletes the web.
        /// </summary>
        /// <param name="parentWebUrl">The parent web URL.</param>
        /// <param name="webUrl">The web URL.</param>
        public static void DeleteWeb(string parentWebUrl, string webUrl)
        {
            using (SPSite site = new SPSite(parentWebUrl))
            {
                using (SPWeb srcWeb = site.OpenWeb(webUrl))
                {
                    SPWebHelper.DeleteWeb(srcWeb, true);
                }
            }
        }

        /// <summary>
        /// Creates a Team Site
        /// </summary>
        /// <param name="parentWebUrl">
        /// The parent web url.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <param name="webUrl">
        /// The web url.
        /// </param>
        public static void CreateTeamSite(string parentWebUrl, string title, string webUrl)
        {
            using (SPSite site = new SPSite(parentWebUrl))
            {
                using (SPWeb parentWeb = site.OpenWeb())
                {
                    // Create a SOURCE test web
                    SPWebHelper.CreateWeb(parentWeb, true, title, "Unit Test Web", "STS#1", webUrl, 1033, false, false);
                }
            }
        }

        public static void CreateMeetingSite(string parentWebUrl, string title, string webUrl)
        {
            using (SPSite site = new SPSite(parentWebUrl))
            {
                using (SPWeb parentWeb = site.OpenWeb())
                {
                    // Create a SOURCE test web
                    SPWebHelper.CreateWeb(parentWeb, true, title, "Unit Test Publishing Web", "MPS#0", webUrl, 1033, false, false);
                }
            }
        }

        /// <summary>
        /// Creates 5 Tests Publishing Pages in the specified list.
        /// </summary>
        /// <param name="web">
        /// The web.
        /// </param>
        public static void CreateTestPages(SPWeb web, string[] pageNames)
        {
            // now lets add our test list 
            PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);

            web.AllowUnsafeUpdates = true;
            for (int i = 0; i < pageNames.Length; i++)
            {
                PublishingPageHelper.CreatePublishingPage(pubWeb, pageNames[i], "WelcomeSplash.aspx", false, true, true);
            }
        }

        /// <summary>
        /// Adds the fields to person list.
        /// </summary>
        /// <param name="newWeb">The new web.</param>
        private static void AddFieldsToPersonList(SPWeb newWeb)
        {

            SPList personList = SPListHelper.GetList(newWeb, TestGlobals.Lists.PersonListName);

            // Add the person Fields to the list
            SPListHelper.AddFieldToList(newWeb, personList, TestGlobals.Fields.Forename,
                                            SPFieldType.Text, false);

            SPListHelper.AddFieldToList(newWeb, personList, TestGlobals.Fields.Surname,
                                            SPFieldType.Text, false);

            SPListHelper.AddFieldToList(newWeb, personList, TestGlobals.Fields.Gender,
                                            SPFieldType.Choice, false);

            // Create a Quick Choice in the new Web
            SPFieldChoice regionField = (SPFieldChoice)personList.Fields[TestGlobals.Fields.Gender];
            regionField.Choices.Add("Male");
            regionField.Choices.Add("Female");
            regionField.DefaultValue = "Male";
            regionField.Update();
        }
    }
}